<?php
/**
 * Created by PhpStorm.
 * User: Dave
 * Date: 2018/5/10
 * Time: 10:59
 */

namespace Biz\tools;

use Doraemon\tools\MiniProgramConfig;
use EasyWeChat\Factory;

class MiniProgramCode
{
    const TP_TYPE_GOODS = [
        'param' => 'goods_id=',
        'page' => 'pages/detail/detail'
    ];
    const TP_TYPE_COLLOCATION = [
        'param' => 'id=',
        'page' => 'pages/spaceDetail/spaceDetail'
    ];
    const BIZ_TYPE_GROUP_BUY = [
        'param' => 'team_id=',
        'page' => 'pages/groupBuy/groupBuy'
    ];
    const BIZ_TYPE_GROUP_BUY_GOODS = [
        'param' => 'goods_id=',
        'page' => 'pages/groupBuyDetail/groupBuyDetail'
    ];

    public static function getMiniProgramCode($type, $id, $uid = NULL)
    {
        switch ($type) {
            case 'groupbuy':
                $typeConfig = self::BIZ_TYPE_GROUP_BUY;
                break;
            default:
                throw new \Exception('类型错误');
                break;
        }
        $app = MiniProgramConfig::getApp(MiniProgramConfig::APP_NAME_BIZ);
        $param = $typeConfig['param'] . $id . ($uid ? '&uid=' . $uid : '');
        $response = $app->app_code->getUnlimit($param, [
            'page' => $typeConfig['page']
        ]);
        if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
            return base64_encode($response->getBodyContents());
        }
        return $response;
    }

    public static function getTpMiniProgramCode($type, $id, $uid = NULL)
    {
        switch ($type) {
            case 'goods':
                $typeConfig = self::TP_TYPE_GOODS;
                break;
            case 'collocation':
                $typeConfig = self::TP_TYPE_COLLOCATION;
                break;
            default:
                throw new \Exception('类型错误');
                break;
        }
        $app = MiniProgramConfig::getApp(MiniProgramConfig::APP_NAME_THE_PARAGRAPH);
        $param = $typeConfig['param'] . $id . ($uid ? '&uid=' . $uid : '');
        $response = $app->app_code->getUnlimit($param, [
            'page' => $typeConfig['page']
        ]);
        if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
            return base64_encode($response->getBodyContents());
        }
        return $response;
    }

    public static function getTpMiniProgramCodeFile($type, $id, $uid = NULL)
    {
        switch ($type) {
            case 'goods':
                $typeConfig = self::TP_TYPE_GOODS;
                break;
            case 'collocation':
                $typeConfig = self::TP_TYPE_COLLOCATION;
                break;
            default:
                throw new \Exception('类型错误');
                break;
        }
        $app = MiniProgramConfig::getApp(MiniProgramConfig::APP_NAME_THE_PARAGRAPH);
        $param = $typeConfig['param'] . $id . ($uid ? '&uid=' . $uid : '');
        $response = $app->app_code->getUnlimit($param, [
            'page' => $typeConfig['page']
        ]);
        if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
            $fileName = $response->saveAs(sys_get_temp_dir(), $type . $id);
            return sys_get_temp_dir() . DIRECTORY_SEPARATOR . $fileName;
        }
        return $response;
    }
}